<?php
// include constants
include "./includes/vars.php";
include "./includes/containers.php";
include "newvars.php";
$pathToRoot = "./"; 		// or "";

?>

<?php
include "header.php";
?>


<?php
// content php code goes here

$link = mysql_connect($dbhost, $dbuser, $dbpass)
or Die("Cannot connect to database server");

mysql_select_db($dbname)
  or Die("Database not found.");


function getDwonloadBookLink1($link){
$pathToRoot = "./"; 		// or "";

	return '<a href="' . $pathToRoot . 'index.php?act=download&BID=' . $link . '" class="link_download">Download this book</a>';
	//return '<A HREF="index.php?act=download&BID=' . $link . '"><img src="images/downloadThisBook.gif" BORDER=0></A>';
}

//added by walid.elswaify on 9 Aug 2008 to support new UI
function getBookImageLink($imageUrl, $altMsg) {
return '<img src="' . $imageUrl . '" alt="' . $altMsg . '" width="84" height="123"/>';
}

// content management

if (isset($_GET['bkID'])){
//view chapter list
    if (!isset($_GET['chapter'])){
    
          // get the book's name
          $sql = "SELECT B_NAME, B_DOWNLOAD_COUNT, B_IMAGE_URL FROM books WHERE B_ID = " . $_GET['bkID'];
          if ($result = mysql_query($sql)){

            $row = mysql_fetch_row($result);
            $bookname = $row[0];
			$bookimage = '<img border="0" width="100" height="150" src="' . $websitename . $row[2] . '"><br>';
			$bookdownload = getDwonloadBookLink1($_GET['bkID']);
			$bookImageLink = getBookImageLink($websitename . $row[2] , $bookname);

			$newcount = $row[1] + 1;
            $sql = "UPDATE books SET B_DOWNLOAD_COUNT = '" . $newcount . "' WHERE B_ID = " . $_GET['bkID'];
            $result = mysql_query($sql);
          }
          else{
              $bookname = "";
          }
      
          // print chapters list
          $sql = "SELECT CHAP_ID, CHAP_NAME FROM chapters WHERE CHAP_BK_ID = " . $_GET['bkID'] . " ORDER BY CHAP_SEQUENCE";

          if ($result = mysql_query($sql)){
              $contenttable = "";
              while ($row = mysql_fetch_row($result)){

                  $contenttable .= '<li><a href="' . $pathToRoot .'test.php?bkID=' . $_GET['bkID'] . '&chapter=' . $row[0] . '">' . $row[1] . '</a></li>'; //to be modified


              }

          }
          else{
              $content = "Sorry, could not get this book's information";
          }
     
      
    }//end of chapter list view
	
        $authsql ="SELECT BA_AUTH_ID FROM bk_auth WHERE BA_BOOK_ID = " . $_GET['bkID'];
    if ($authresult = mysql_query($authsql)){
        $authrow = mysql_fetch_row($authresult);
        $authsql2 ="SELECT * FROM authors WHERE A_ID = " . $authrow[0];
        if($authresult2 = mysql_query($authsql2)){
            $authrow2 = mysql_fetch_row($authresult2);
        }		if($authrow2[2]){
			$authorname .= 'By: <a href="' . $pathToRoot . 'view.php?authID=' . $authrow2[0] . '">' . $authrow2[1] . " " . $authrow2[2] . '</a><BR></P>';
		}    }      
  
}



mysql_close ($link);

		
?>
<!-- content html code goes here-->
              <td rowspan="2" class="content_center" valign="top">
			  		<div class="content_header">
					<table width="100%" border="0" cellpadding="5" cellspacing="0" bgcolor="#ecece7" class="chapters_header">
					  <tr>
						<td width="100%" valign="top"><div class="content_header_image float_left">
							<?php print $bookImageLink;?>
						</div>
						<div class="content_header_details">
							<h2><?php print $bookname;?></h2>
							
							<br>
							<?php print $authorname;?>
							
							<p class="blue">
								<strong>Read by:</strong> <?php print $newcount;?><br />
							<p>

						<?php print $bookdownload;?>
								</p>
						</div></td>
						

					  </tr>
					</table>
					<div class="clear"></div>
					</div>
				<div class="content_body">
				  <ul>
				   <?php print $contenttable;?>
				  </ul>

				</div>			  </td>
<!-- end of content html code-->



<?php
include "footer.php";
?>
